﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MainGameModule.ViewModels;
using System.Collections.ObjectModel;
using BusinessObjects;
using YourWorld.Common.Infrastucture.Events;
using Microsoft.Expression.Controls;

namespace MainGameModule.Views
{
    public partial class PopUpTrashView : UserControl
    {
        private double x = 326;
        private double y = 144;

        public PopUpTrashView( PopUpTrashViewModel dataContext )
        {
            InitializeComponent();

            this.DataContext = dataContext;

            this.BinsEndStoryboard.Completed += new EventHandler( BinsEndStoryboard_Completed );
            EventService.Aggregator.GetEvent<ChooseBinAnswerEvent>().Subscribe( this.ReactToAnswer );
        }
        
        public void ReactToAnswer( bool isRight )
        {
            if ( isRight == true )
                this.ShowRightAnswer();
            else
                this.ShowWrongAnswer();
        }

        private void UserControl_Loaded( object sender, RoutedEventArgs e )
        {
            this.pathListBox.LayoutPaths[0].Span = 6;
            this.WrongAnswerFeedbackGrid.Visibility = Visibility.Collapsed;
            this.RightAnswerFeedbackGrid.Visibility = Visibility.Collapsed;

            //this.RecyclingAnimation.Begin();
            this.BinsStoryboard.Begin();

            this.TryAgainStoryboard.Completed += new EventHandler( TryAgainStoryboard_Completed );
            this.ShowWriteAnswerAndCloseStoryboard.Completed += new EventHandler( ShowWriteAnswerAndCloseStoryboard_Completed );

        }

        private void ShowWrongAnswer()
        {
            this.TryAgainStoryboard.Begin();
        }

        void TryAgainStoryboard_Completed( object sender, EventArgs e )
        {
            this.WrongAnswerFeedbackGrid.Visibility = Visibility.Collapsed;
            this.RightAnswerFeedbackGrid.Visibility = Visibility.Collapsed;
        }

        private void ShowRightAnswer()
        {
            this.ShowWriteAnswerAndCloseStoryboard.Begin();
        }

        private void ShowWriteAnswerAndCloseStoryboard_Completed( object sender, EventArgs e )
        {
            try
            {
                this.beh.X = this.x;
                this.beh.Y = this.y;
            }
            catch 
            {
                int debug = 1;
            }

            this.WrongAnswerFeedbackGrid.Visibility = Visibility.Collapsed;
            this.RightAnswerFeedbackGrid.Visibility = Visibility.Collapsed;

            ( this.DataContext as PopUpTrashViewModel ).CloseCommand.Execute( null );
        }

        private void BinsEndStoryboard_Completed( object sender, EventArgs e )
        {
        }

        private void Button_Click( object sender, RoutedEventArgs e )
        {
        }

        private void beh_DragFinished( object sender, MouseEventArgs e )
        {
            PathListBoxItem pli;
            Point p;

            foreach ( KeyValuePair<object, DependencyObject> element in this.pathListBox.GetItemsAndContainers().ToArray() )
            {
                pli = element.Value as PathListBoxItem;
                p = e.GetPosition( pli );

                if ( p.X > 0 && p.Y > 0 && p.X < pli.ActualWidth && p.Y < pli.ActualHeight )
                {
                    BinViewModel bin = element.Key as BinViewModel;
                    bin.SelectBinCommand.Execute( null );
                }
            }
        }

        private void Path_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            this.pathListBox.SelectedItem = null;
        }

    }
}
